package com.gallsoft.games.bennyadv;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import com.gallsoft.games.framework.Game;
import com.gallsoft.games.framework.Input.TouchEvent;
import com.gallsoft.games.framework.gamedev2d.SpriteBatcher;
import com.gallsoft.games.framework.gl.Camera2D;
import com.gallsoft.games.framework.impl.GLScreen;
import com.gallsoft.games.framework.math.OverlapTester;
import com.gallsoft.games.framework.math.Rectangle;
import com.gallsoft.games.framework.math.Vector2;

public class MainMenuScreen extends GLScreen {

    Camera2D guiCam;
    SpriteBatcher batcher;
    Rectangle soundBounds;
    Rectangle playBounds;
    Rectangle highscoresBounds;
    Rectangle helpBounds;
    Vector2 touchPoint;

    public MainMenuScreen(Game game) {
        super(game);
        guiCam = new Camera2D(glGraphics, 480, 320);
        batcher = new SpriteBatcher(glGraphics, 100);
        //soundBounds = new Rectangle(0, 0, 64, 64);
        //playBounds = new Rectangle(160 - 150, 200 + 18, 300, 36);
        //playBounds = new Rectangle(236 - 120, 76 - 20, 240, 40);
        playBounds = new Rectangle(230, 70, 180, 30);
        //highscoresBounds = new Rectangle(160 - 150, 200 - 18, 300, 36);
        //helpBounds = new Rectangle(160 - 150, 200 - 18 - 36, 300, 36);
        touchPoint = new Vector2();
    }

    @Override
    public void update(float deltaTime) {
        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();

        int len = touchEvents.size();
        for (int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            
            if (event.type == TouchEvent.TOUCH_UP) {
                touchPoint.set(event.x, event.y);
                guiCam.touchToWorld(touchPoint);

                if (OverlapTester.pointInRectangle(playBounds, touchPoint)) {
                    Assets.playSound(Assets.clickSound);
                    game.setScreen(new GameScreen(game));
                    return;
                }

                /*
                if (OverlapTester.pointInRectangle(highscoresBounds, touchPoint)) {
                    Assets.playSound(Assets.clickSound);
                    game.setScreen(new HighscoresScreen(game));
                    return;
                }

                if (OverlapTester.pointInRectangle(helpBounds, touchPoint)) {
                    Assets.playSound(Assets.clickSound);
                    game.setScreen(new HelpScreen(game));
                    return;
                }

                if (OverlapTester.pointInRectangle(soundBounds, touchPoint)) {
                    Assets.playSound(Assets.clickSound);
                    Settings.soundEnabled = !Settings.soundEnabled;
                    if (Settings.soundEnabled)
                        Assets.music.play();
                    else
                        Assets.music.pause();
                }
                */
            }
        }
    }

    @Override
    public void present(float deltaTime) {
        GL10 gl = glGraphics.getGL();
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        guiCam.setViewportAndMatrices();

        gl.glEnable(GL10.GL_TEXTURE_2D);

        batcher.beginBatch(Assets.titlescreen);
        batcher.drawSprite(240, 160, 480, 320, Assets.titlescreenRegion);
        batcher.endBatch();

        /*
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        
        batcher.beginBatch(Assets.background);
        batcher.drawSprite(240, 210, 410, 100, Assets.title);
        batcher.drawSprite(236, 76, 240, 40, Assets.start);
        batcher.endBatch();
        
        gl.glDisable(GL10.GL_BLEND);
        */
    }

    @Override
    public void pause() {
        Settings.save(game.getFileIO());
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
    }
}
